#include "stdafx.h"
#include <XInitializationFile.h>

void XInitializationFile::LoadFile(const char* path, int bufferSize)
{
	std::fstream file;
	file.open(path, std::ios::in);

	char* fileData = new char[bufferSize];
	file.read(fileData, bufferSize);
	FileString.assign(fileData);
	delete[] fileData;

	for(int i=0; i<FileString.length(); i++)
	{
		if(FileString[i] == 32 || FileString[i]<0 || FileString[i] == 34)
		{
			do
			{
				FileString.erase(FileString.begin() + i);

			}while((FileString[i] == 32 || FileString[i]<0 || FileString[i] == 34) && i<FileString.length()-1);
		}
	}

	int a = 0;
}

float XInitializationFile::GetFloat(std::string key, std::string section)
{
	int start = 0;
	int end = 0;

	if(section.length() >1)
	{
		start = FileString.find(section);

		if(start != -1)
		{
			int sec = start;
			start = FileString.find(key, start);

			for(int i=sec+section.length()+1; i<start; i++)
			{
				if(FileString[i] == 91 || FileString[i] == 93)
					return -1;
			}

			if(start != -1)
			{
				for(int i=start+key.length()+1; i<FileString.size(); i++)
				{
					if(FileString[i] == 10)
					{
						end = i;
						break;
					}
				}
			}
		}
		else
			return -1;
			
	}
	else
	{
		start = FileString.find(key);

			if(start != -1)
			{
				for(int i=start+key.length()+1; i<FileString.size(); i++)
				{
					if(FileString[i] == 10)
					{
						end = i;
						break;
					}
				}
			}
			else
				return -1;
	}

	char* width = new char[end-start-key.length()-1];
	FileString.copy(width, end-start-key.length()-1, start+key.length()+1);
	return atof(width);
}

int XInitializationFile::GetInt(std::string key, std::string section)
{
	int start = 0;
	int end = 0;

	if(section.length() >1)
	{
		start = FileString.find(section);

		if(start != -1)
		{
			int sec = start;
			start = FileString.find(key, start);

			for(int i=sec+section.length()+1; i<start; i++)
			{
				if(FileString[i] == 91 || FileString[i] == 93)
					return -1;
			}

			if(start != -1)
			{
				for(int i=start+key.length()+1; i<FileString.size(); i++)
				{
					if(FileString[i] == 10)
					{
						end = i;
						break;
					}
				}
			}
		}
		else
			return -1;
			
	}
	else
	{
		start = FileString.find(key);

			if(start != -1)
			{
				for(int i=start+key.length()+1; i<FileString.size(); i++)
				{
					if(FileString[i] == 10)
					{
						end = i;
						break;
					}
				}
			}
			else
				return -1;
	}

	char* width = new char[end-start-key.length()-1];
	FileString.copy(width, end-start-key.length()-1, start+key.length()+1);
	return atoi(width);
}

std::string XInitializationFile::GetString(std::string key, std::string section)
{
	int start = 0;
	int end = 0;

	if(section.length() >1)
	{
		start = FileString.find(section);


		if(start != -1)
		{
			int sec = start;
			start = FileString.find(key, start);

			for(int i=sec+section.length()+1; i<start; i++)
			{
				if(FileString[i] == 91 || FileString[i] == 93)
					return "";
			}

			if(start != -1)
			{
				for(int i=start+key.length()+1; i<FileString.size(); i++)
				{
					if(FileString[i] == 10)
					{
						end = i;
						break;
					}
				}
			}
		}
		else
			return "";
			
	}
	else
	{
		start = FileString.find(key);

			if(start != -1)
			{
				for(int i=start+key.length()+1; i<FileString.size(); i++)
				{
					if(FileString[i] == 10)
					{
						end = i;
						break;
					}
				}
			}
			else
				return "";
	}

	char* width = new char[end-start-key.length()-1];
	FileString.copy(width, end-start-key.length()-1, start+key.length()+1);

	std::string str;
	str.assign(width, end-start-key.length()-1);

	return str;
}